<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2019/12/29
 * Time: 17:10
 */

namespace app\command\controller;

use app\api\controller\Order;
use app\mtadminapi\model\MtOrder;
use think\config\driver\Json;
use think\Controller;
use app\model\mt_user;

/**
 * 生成器
 * Class Generate
 * @package app\command\controller
 */
class Generate extends Controller
{
    /**
     * @param string $openID
     * @return string
     * 生成二十二位的唯一邀请码
     */
    public function produceInviteCode($openID = ""){
        static $inviteCode = '';
        $uid = uniqid("", true);
        $data = $openID;
        $data .= $_SERVER['REQUEST_TIME'];
        $data .= $_SERVER['HTTP_USER_AGENT'];
//        $data .= $_SERVER['LOCAL_ADDR'];
//        $data .= $_SERVER['LOCAL_PORT'];
        $data .= $_SERVER['REMOTE_ADDR'];
        $data .= $_SERVER['REMOTE_PORT'];
        $hash = strtoupper(hash('ripemd128', $uid . $inviteCode . md5($data)));
        $inviteCode = substr($hash, 0, 2) . substr($hash, 8, 4) .substr($hash, 20, 10);
        $repeat = (new mt_user())->where(['inviteCode'=>$inviteCode])->find();
        if($repeat){
            $this->produceInviteCode($openID);
        }
        return $inviteCode;
    }
    /**
     * 密码生成(php password_hash方法)
     * @param $password
     * @return bool|string
     */
    public function passwordHash($password){
        $password = password_hash($password,PASSWORD_BCRYPT);
        return $password;
    }

    /**
     * token生成
     * @param $id
     * @param $data
     * @return string
     */
    public function createToken($id, $data = ""){
        $dataArr = [
            "id" => $id,
            "data" => $data,
            "time" => time(),
            "unionid" => uniqid(),
        ];
        $token = md5(md5(base64_encode(json_encode($dataArr,JSON_UNESCAPED_UNICODE))));
        return $token;
    }

    /**
     * 订单号生成
     * @return string
     */
    public function orderNumber(){
        $orderNumber = date('YmdHis').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $repeat = (new MtOrder())->where("orderNo",$orderNumber)->count();
        if($repeat){
            $this->orderNumber();
        }
        return $orderNumber;
    }

    /**
     * 优惠券生成
     * @param $no_of_codes
     * @param string $exclude_codes_array
     * @param int $code_length
     * @return array
     */
    public function generate_promotion_code($no_of_codes, $exclude_codes_array = '', $code_length = 10) {
        $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $promotion_codes = array(); //这个数组用来接收生成的优惠码
        for ($j = 0; $j < $no_of_codes; $j++) {
            $code = "";
            for ($i = 0; $i < $code_length; $i++) {
                $code.= $characters[mt_rand(0, strlen($characters) - 1) ];
            }
            //如果生成的4位随机数不再我们定义的$promotion_codes函数里面
            if (!in_array($code, $promotion_codes)) {
                if (is_array($exclude_codes_array)) //
                {
                    if (!in_array($code, $exclude_codes_array)) //排除已经使用的优惠码
                    {
                        $promotion_codes[$j] = $code;
                        //将生成的新优惠码赋值给promotion_codes数组
                } else {
                        $j--;
                    }
                } else {
                    $promotion_codes[$j] = $code; //将优惠码赋值给数组

                }
            } else {
                $j--;
            }
        }
        return $promotion_codes;
    }
}